package com.cys.exam.${package}.service.impl;

import com.cys.common.util.base.IdWorker;
import com.cys.common.util.base.MapUtil;
import com.cys.common.util.base.StringUtil;
import com.cys.exam.${package}.entity.${entityName};
import com.cys.exam.interfaces.dao.${package}.${entityName}Dao;
import com.cys.exam.interfaces.entity.${package}.I${entityName};
import com.cys.exam.interfaces.service.${package}.${entityName}Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import javax.annotation.Resource;

/**
 *  ${entityComment} Service接口实现类
 * 	@author ${author}
 */
@Service("${entityNameSmall}ServiceImpl")
public class ${entityName}ServiceImpl implements ${entityName}Service{
	
	private static Logger logger = LoggerFactory.getLogger(${entityName}ServiceImpl.class);

	@Autowired
    private ${entityName}Dao ${entityNameSmall}Dao;
	
	
	/**
	 *  新增${entityName}
	 *  @param params ${entityName}实体类对应参数
	 */
	@Override
    public I${entityName} save(Map<String, Object> params) throws Exception {
		if(!StringUtil.checkObj(params)){
			logger.error("method[save] params is null.");
			return null;
		}
    	I${entityName} ${entityNameSmall} = (I${entityName})MapUtil.toObject(${entityName}.class,params);
<#list dataList as var>
	<#if var_index == 0>
		${entityNameSmall}.set${var.newGetSetCode}(IdWorker.getId());
	</#if>
</#list>
    	${entityNameSmall}Dao.save(${entityNameSmall});
    	return ${entityNameSmall};
    }
    
	/**
	 *	修改${entityName}
	 *  @param params ${entityName}实体类对应参数
	 */
	@Override
	public I${entityName} update(Map<String, Object> params) throws Exception {
		if(!StringUtil.checkObj(params)){
			logger.error("method[update] params is null.");
			return null;
		}
<#list dataList as var>
	<#if var_index == 0>
		Long id = MapUtil.getLong(params,"${var.newColumn}");
	</#if>
</#list>
		if(!StringUtil.checkObj(id)){
			logger.error("method[update] params.id is null.");
			return null;
		}
		I${entityName} ${entityNameSmall} = (I${entityName})MapUtil.toObject(${entityName}.class,params);
		${entityNameSmall}Dao.update(${entityNameSmall});
		return ${entityNameSmall};
	}
	
	/**
	 *  通过id物理删除${entityName}
	 *  @param id ${entityName}主键
	 */
	@Override
	public void deleteById(Long id){
		if(!StringUtil.checkObj(id)){
			logger.warn("method[deleteById] id is null.");
			return ;
		}
		${entityNameSmall}Dao.deleteById(id);
	}
	
	/**
	 *  批量物理删除${entityName}
	 *  @param ids ${entityName}主键数组
	 */
	@Override
	public void deleteBatch(Long[] ids){
		if(!StringUtil.checkObj(ids)){
			logger.warn("method[deleteBatch] ids is null.");
			return ;
		}
		${entityNameSmall}Dao.deleteBatch(ids);
	}
	
	/**
	 *  通过id逻辑删除${entityName}
	 *  @param id ${entityName}主键
	 */
	@Override
	public void logicDeleteById(Long id){
		if(!StringUtil.checkObj(id)){
			logger.warn("method[logicDeleteById] id is null.");
			return ;
		}
		${entityNameSmall}Dao.logicDeleteById(id);
	}
	
	/**
	 *  批量逻辑删除${entityName}
	 *  @param ids ${entityName}主键数组
	 */
	@Override
	public void logicDeleteBatch(Long[] ids){
		if(!StringUtil.checkObj(ids)){
			logger.warn("method[logicDeleteBatch] ids is null.");
			return ;
		}
		${entityNameSmall}Dao.logicDeleteBatch(ids);
	}
	
	
	/**
	 *  根据id加载${entityName}
	 *  @param id ${entityName}主键
	 */
	@Override
	public I${entityName} findEntityById(Long id){
		if(!StringUtil.checkObj(id)){
			logger.warn("method[find] id is null.");
			return null;
		}
		return ${entityNameSmall}Dao.findEntityById(id);
	}

	/**
	 *  获取${entityName}列表数据
	 *	@param map 默认空数据集
	 */
	@Override
	public List<I${entityName}> findList(Map<String,Object> map){
		return (List<I${entityName}>)${entityNameSmall}Dao.findList(map);
	}


	/**
	 * 分页获取数据
	 *
	 * @param pageNo   当前页
	 * @param pageSize 当前页数
	 * @param params   检索参数
	 */
	@Override
	public PageInfo<I${entityName}> findPage(Integer pageNo, Integer pageSize, Map<String, Object> params) {
		if (!StringUtil.checkObj(pageNo)) {
			logger.warn("method[findPage] pageNo is null. pageNo = 1");
			pageNo = 1;
		}
		if (!StringUtil.checkObj(pageSize)) {
			logger.warn("method[findPage] pageSize is null. pageSize = 5");
			pageSize = 5;
		}
		PageHelper.startPage(pageNo, pageSize);
		List<I${entityName}> dataList = ${entityNameSmall}Dao.findPage(params);
		PageInfo<I${entityName}> pageInfo = new PageInfo<>(dataList);
		return pageInfo;
	}

}
 



